export default class EventEmitter {
  constructor(options = {}) {
    this.cbs = {}
  }
  on(evtName, callback) {
    if (!this.cbs[evtName]) {
      this.cbs[evtName] = []
    }
    this.cbs[evtName].push(callback)
    return this
  }
  emit(evtName, data) {
    const args = Array.prototype.slice.call(arguments, 1)
    let cbs = this.cbs[evtName] || []
    cbs.forEach((cb) => {
      cb.apply(null, args)
    })
    return this
  }
  off(evtName, callback) {
    let cbs = this.cbs[evtName]
    if (cbs) {
      let index = cbs.indexOf(callback)
      if (index > -1) {
        cbs.splice(index, 1)
      }
    }
    return this
  }
  destroy() {
    this.cbs = null
    return this
  }
}
